package com.weiqk.pmdemo.service.impl;

import com.weiqk.pmdemo.dao.*;
import com.weiqk.pmdemo.dto.*;
import com.weiqk.pmdemo.pojo.*;
import com.weiqk.pmdemo.service.*;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ProjectServiceImpl implements ProjectService {
    @Autowired
    PurchaseRepository purchaseRepository;
    @Autowired
    ProjectRepository projectRepository;
    @Autowired
    ApplyLinkRepository applyLinkRepository;
    @Autowired
    PurchaseService purchaseService;
    @Autowired
    ApplyLinkService applyLinkService;



    @Override
    public Iterable<Project> findAll() {
        return projectRepository.findAll();
    }

    @Override
    public Boolean deleteById(int id) {
        boolean apply = applyLinkRepository.findById(id).isPresent();
        boolean purchase = purchaseRepository.findById(id).isPresent();
        if (apply){
            return false;
        }
        if (purchase){
            return false;
        }
        return projectRepository.findById(id).isPresent();
    }

    @Override
    public Boolean update(Project project) {
        //更改purchase数据
        List<Purchase> ps = purchaseRepository.findOneById(project.getProjectId());
        for (Purchase p:ps){
            PurchaseInDto pdto = new PurchaseInDto();
            pdto.setProjectNum(project.getProjectId());
            pdto.setPurchase(p);
            purchaseService.update(pdto);
        }
        //更改paylink数据
        List<ApplyLink> as = applyLinkRepository.findOneById(project.getProjectId());
        for (ApplyLink a:as){
            ApplyLinkInDto adto = new ApplyLinkInDto();
            adto.setProjectNum(project.getProjectId());
            adto.setApplyLink(a);
            applyLinkService.update(adto);
        }
        //执行更新
        //处理时间
        project.setUpadteTime(new Date());
        projectRepository.save(project);
        return true;
    }

    @Override
    public Boolean put(Project project) {
        //处理时间
        project.setUpadteTime(new Date());
        project.setCreateTime(new Date());
        projectRepository.save(project);
        return true;
    }

    @Override
    public int counttruenum(int id) {
        List<Purchase> purchases = purchaseRepository.findOneById(id);
        return  purchases.stream().mapToInt(Purchase::getActualAmount).sum();
    }

    @Override
    public void saveAll(List<Project> list) {
        Iterable<Project> ts = ProjectServiceImpl.listConversIterable(list);
        for (Project  project:ts){
            project.setUpadteTime(new Date());
            project.setCreateTime(new Date());
        }
        projectRepository.saveAll(ts);
    }


    //工具类
    static <Project> Iterable<Project> listConversIterable(List<Project> list){

        return new Iterable<Project>() {

            @Override
            public Iterator<Project> iterator() {
                return new Iterator<Project>() {

                    ListIterator<Project> listIterator = list.listIterator(list.size());
                    @Override
                    public boolean hasNext() {

                        return listIterator.hasPrevious();
                    }
                    @Override
                    public Project next() {
                        return listIterator.previous();
                    }
                    @Override
                    public void remove() {
                        listIterator.remove();
                    }
                };
            }
        };

    }

}
